import useUserStore from "@/stores/modules/user";

/**
 * perm 操作权限处理
 * 指令用法：
 *  <el-button v-perms="['user.edit']">编辑</el-button>
 */
export default {
    mounted: (el, binding) => {
        const {value} = binding
        const userStore = useUserStore()
        const permissions = userStore.perms
        if (Array.isArray(value)) {
            if (value.length > 0) {
                let hasPermission = true
                if (!permissions.includes('*')) {
                    value.forEach((item) => {
                        if (!permissions.includes(item)) {
                            hasPermission = false
                        }
                    })
                }

                if (!hasPermission) {
                    el.parentNode && el.parentNode.removeChild(el)
                }
            }
        } else {
            throw new Error('like v-perms="[\'Role.list\']"')
        }
    }
}